package cn.thrjit.template;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.thrjit.properties.AppSettingProperties;

public class AppWeChatTemplate {

    private AppSettingProperties appSettingProperties;
    // 把方法里的appSettingProperties传给全局变量appSettingProperties，扩大appSettingProperties的作用域
    public AppWeChatTemplate(AppSettingProperties appSettingProperties) {
        this.appSettingProperties = appSettingProperties;
    }

    /**
     * wx小程序获取accessToken
     * @return
     */
    public String getAccessTokenByCode(){
        // phoneCode校验 获取accessToken
        // 依次把参数替换到请求地址里的%s中，构建出完整的请求URL
        String accessUrl = String.format(appSettingProperties.getAccessTokenToUrl(),appSettingProperties.getAppId(),appSettingProperties.getSecret());
        // 发请求
        String resultJsonStr = HttpUtil.get(accessUrl);
        // 把返回的json字符串转围殴json对象
        JSONObject accessUrlResult = JSONUtil.toBean(resultJsonStr, JSONObject.class);
        // 从对象中拿到accessToken
        String accessToken = accessUrlResult.getStr("access_token");
        // 校验accessToken
        if(StrUtil.isEmpty(accessToken)){
            throw new RuntimeException("获取accessToken失败");
        }
        return accessToken;
    }

    /**
     * wx小程序根据accessToken获取手机号
     * @param code
     * @return
     */
    public String getPhoneByAccess(String code){
        // 调用方法获取accessToken
        String accessToken = this.getAccessTokenByCode();
        // 获取手机号
        String phoneToUrl = String.format(appSettingProperties.getPhoneToUrl(), accessToken);
        JSONObject body = new JSONObject();
        body.set("code",code);
        String phoneData = HttpUtil.post(phoneToUrl, body.toString());// 转换为json字符串再发送
        JSONObject phoneObjData = JSONUtil.parseObj(phoneData);
        Integer errcod = phoneObjData.getInt("errcod");// 获取返回的状态码
        if (errcod != 0){
            throw new RuntimeException("获取电话号状态不通过，请重试");
        }
        JSONObject phoneInfo = phoneObjData.getJSONObject("phone_info");
        String phoneNumber = phoneInfo.getStr("phoneNumber");
        if (StrUtil.isEmpty(phoneNumber)){
            throw new RuntimeException("获取电话号码失败");
        }
        return phoneNumber;
    }

    /**
     * wx小程序根据code获取openId
     * @param code
     * @return
     */
    public String getOpenIdByCode(String code){
        String urlTemplate = appSettingProperties.getOpenIdUrl();
        String url = String.format(urlTemplate,appSettingProperties.getAppId(),appSettingProperties.getSecret(),code);
        String jsonResult  = HttpUtil.get(url);
        if (StrUtil.isEmpty(jsonResult)){
            throw new RuntimeException("获取openId失败");
        }
        JSONObject jsonObject = JSONUtil.parseObj(jsonResult);
        String openId = jsonObject.getStr("openid");
        if (StrUtil.isEmpty(openId)){
            throw new RuntimeException("获取openId失败");
        }
        return openId;
    }
}
